﻿using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.PageActionManager;
using ZBJF.Warranty.WebProvider;

namespace ZBJF.Warranty.AdminSite.Areas.System.Controllers
{
    public class PageActionController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Index(BaseQueryParams model)
        {
            int rowCount = 0;
            var result = ServiceFactory.PageActionServiceInstance.GetPagingList(model, out rowCount).Select(o => new
            {
                o.ID,
                o.ActionCode,
                o.ActionName,
                o.ActionNameEN,
                o.ActionNameRU,
                o.IsAvailable,
                o.OrderIndex
            });

            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = rowCount,
                iTotalDisplayRecords = rowCount,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取所有的页面及其权限
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetPageToAction(PageToActionModel model)
        {
            int total;
            var list = ServiceFactory.PageToActionServiceInstance.GetPagingList(model, out total).Include(o => o.PageMenu).Include(o => o.PageAction).OrderBy(o => o.PageMenu.BelongApplicationType).ThenByDescending(o => o.PageID).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.ActionID,
                PageName = ServiceFactory.PageMenuServiceInstance.GetFullPageMenuName(o.PageMenu.ID),
                o.PageMenu.BelongApplicationType,
                BelongApplicationTypeDesc = EnumHelper.Instance.GetDescription(typeof(ApplicationType), o.PageMenu.BelongApplicationType),
                o.PageMenu.PageUrl,
                o.PageAction.ActionName,
                o.PageAction.ActionCode,
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var result = ServiceFactory.PageActionServiceInstance.Load(id);
            return View(result);
        }

        /// <summary>
        /// 添加页面按钮
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(Model.PageAction model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            if (ModelState.IsValid)
            {
                if (model.ID > 0)
                {
                    if (ServiceFactory.PageActionServiceInstance.IsExistActionName(model.ActionName, model.ID))
                    {
                        responseResult.Message = "页面按钮名称已经存在了";

                    }
                    else if (ServiceFactory.PageActionServiceInstance.IsExistActionCode(model.ActionCode, model.ID))
                    {
                        responseResult.Message = "页面按钮代码已经存在了";
                    }
                    else
                    {
                        var pageAction = ServiceFactory.PageActionServiceInstance.Load(model.ID);
                        if (pageAction != null)
                        {
                            pageAction.ActionCode = model.ActionCode;
                            pageAction.OrderIndex = model.OrderIndex;
                            pageAction.IsAvailable = model.IsAvailable;
                            pageAction.ActionName = model.ActionName;
                            pageAction.ActionNameEN = model.ActionNameEN;
                            pageAction.ActionNameRU = model.ActionNameRU;
                            responseResult.Success = ServiceFactory.PageActionServiceInstance.UpdateEntities(pageAction);
                        }
                        else
                        {
                            responseResult.Message = "按钮已经不存在了";
                        }
                    }
                }
                else
                {
                    if (ServiceFactory.PageActionServiceInstance.IsExistActionName(model.ActionName))
                    {
                        responseResult.Message = "页面按钮名称已经存在了";

                    }
                    else if (ServiceFactory.PageActionServiceInstance.IsExistActionCode(model.ActionCode))
                    {
                        responseResult.Message = "页面按钮代码已经存在了";
                    }
                    else
                    {
                        var entity = ServiceFactory.PageActionServiceInstance.AddEntities(model);
                        responseResult.Success = entity.ID > 0;
                    }
                }
            }
            else
            {
                responseResult.Message = "信息不完整";
            }
            if (responseResult.Success)
                CacheHelper.Instance.RemoveAllCache();

            TempData["responseResult"] = responseResult;
            return RedirectToAction("Edit", new { id = model.ID });
        }

        /// <summary>
        /// 更改按钮状态
        /// </summary>
        /// <param name="id">按钮Id</param>
        /// <param name="isAvailable">按钮状态</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateStatus(int id = 0, bool isAvailable = false)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = OpreateType.Update
            };
            var entity = ServiceFactory.PageActionServiceInstance.Load(id);
            if (entity != null)
            {
                entity.IsAvailable = isAvailable;
                responseResult.Success = ServiceFactory.PageActionServiceInstance.UpdateEntities(entity);
                CacheHelper.Instance.RemoveAllCache();
            }
            else
            {
                responseResult.Message = "找不到该按钮";
            }
            return Json(new
            {
                success = responseResult.Success,
                message = responseResult.Message
            });
        }

        [HttpPost]
        public JsonResult DeletePageAction(int id = 0)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = OpreateType.Delete
            };
            var entity = ServiceFactory.PageActionServiceInstance.Load(id);
            if (entity != null)
            {
                if (ServiceFactory.PageToActionServiceInstance.LoadEntities(p => p.ActionID == id).Any())
                {
                    responseResult.Message = "该按钮已绑定到页面，请先从页面中去除该按钮";
                }
                else
                {
                    responseResult.Success = ServiceFactory.PageActionServiceInstance.DeleteEntities(entity);
                    CacheHelper.Instance.RemoveAllCache();
                }
            }
            else
            {
                responseResult.Message = "该按钮已不存在";
            }
            return Json(new
            {
                success = responseResult.Success,
                message = responseResult.Message
            });
        }
    }
}
